/*
 * Copyright 2010 Kevin L'Huillier <klhuillier@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.lhuillier.pwsafe.model;


import java.util.Date;


/**
 * An immutable value object for a single entry in a password history for a
 * record.
 * <p>
 * An entry contains both the password and the time it was set. Because this
 * history will never change, an entry is immutable and provides no means of
 * creating a new value with a different property.
 */
public class PasswordHistoryEntry {
    private final Date timeSet;
    private final String password;

    /**
     * @throws NullPointerException if timeSet is null
     */
    public PasswordHistoryEntry(Date timeSet, String password) {
        this.timeSet = (Date) timeSet.clone();
        this.password = password;
    }

    public Date getTimeSet() {
        return (Date) timeSet.clone();
    }

    public String getPassword() {
        return password;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        } else if (obj == null || !getClass().equals(obj.getClass())) {
            return false;
        }
        PasswordHistoryEntry other = (PasswordHistoryEntry) obj;
        return password.equals(other.password) && timeSet.equals(other.timeSet);
    }

    @Override
    public int hashCode() {
        return timeSet.hashCode() * 31 + password.hashCode();
    }

    @Override
    public String toString() {
        return getClass().getName() + "[timeSet=" + timeSet
                + ";password=" + password + "]";
    }
}
